﻿using System;
using System.ServiceModel;
using System.Windows;
using VideoChatClient.VideoChatServiceReference;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Threading;

namespace VideoChatClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Dispatcher CurrentDisp = Dispatcher.CurrentDispatcher;
        
        public MainWindow()
        {
            InitializeComponent();
            Loaded += (_1, _2) => { DataContext = App.ViewModel; };
            Closing += (_1, _2) => { Environment.Exit(0); };
        }

        private void WebCam_NewVideoSample(object sender, WPFMediaKit.DirectShow.MediaPlayers.VideoSampleArgs e)
        {
            MemoryStream ms = new MemoryStream();
            e.VideoFrame.RotateFlip(RotateFlipType.Rotate180FlipX);
            e.VideoFrame.Save(ms, ImageFormat.Jpeg);
            CurrentDisp.BeginInvoke( (Action<byte[]>) (t => { App.ViewModel.MyVideoFrame = t.Clone() as byte[]; }), ms.GetBuffer() as byte[]);
        }

        private void tgbtnSendMedia_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.WebCam.Play();
            App.ViewModel.IsMediaSending = true;
        }

        private void tgbtnSendMedia_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.WebCam.Stop();
            App.ViewModel.IsMediaSending = false;
        }
    }
}
